package com.example.parttimejob.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.parttimejob.mapper.chatmapper;
import com.example.parttimejob.pojo.entity.chat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class chatcontroller {
    @Autowired
    com.example.parttimejob.mapper.chatmapper chatmapper;
    @GetMapping("/chat/back_nameanduser_name/{user_name}/{back_name}")
    public List<chat> getChatFromTwo(@PathVariable String user_name, @PathVariable String back_name) {
        QueryWrapper<chat> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(qw -> qw.eq("back_name", back_name).eq("user_name", user_name))
                .or(qw -> qw.eq("back_name", user_name).eq("user_name", back_name));
        return chatmapper.selectList(queryWrapper);
    }
    @DeleteMapping("/chat/user_name/{user_name}")
    public String deleteChatByUsername(@PathVariable String user_name) {
        QueryWrapper<chat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", user_name);
        int deletedCount = chatmapper.delete(queryWrapper);
        return "成功删除 " + deletedCount + " 条记录";
    }

    @DeleteMapping("/chat/back_name/{back_name}")
    public String deleteChatByBackname(@PathVariable String back_name) {
        QueryWrapper<chat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("back_name", back_name);
        int deletedCount = chatmapper.delete(queryWrapper);
        return "成功删除 " + deletedCount + " 条记录";
    }
    @DeleteMapping("/chat/delete/{user_name}/{back_name}")
    public String deleteChatByUserAndBackName(@PathVariable String user_name, @PathVariable String back_name) {
        QueryWrapper<chat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", user_name)
                .and(wrapper -> wrapper.eq("back_name", back_name));
        int deletedCount = chatmapper.delete(queryWrapper);
        return "成功删除 " + deletedCount + " 条记录";
    }
    @PostMapping("/chat")
    public chat createchat(@RequestBody chat user) {
        chatmapper.insert(user);
        return user;
    }
}
